package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Date;
import java.util.List;

import longcaisuyun.longcai.com.suyunbean.QiangDanItem;
import longcaisuyun.longcai.com.utils.TimerUtils;
import wh.database.KJDB;



/**
 * Created by Administrator on 2016/4/15.
 */
public class LimitAdapter extends BaseAdapter {
    private List<QiangDanItem> list;
    private Context context;
    private boolean isPlay;
    TextView textView;
    KJDB kjdb;
    Date begin;
    String showDate;
    String str;

    private Handler handler = new Handler();
    ViewHolder v;
    private Runnable runnable = new Runnable() {

        @Override
        public void run() {
            if (!isPlay){
                return;
            }else{
                handler.postDelayed(this, 1000);//开始计时，每隔1秒刷新一次时间
            }
            Date end = null;
            for (int j = 0; j < list.size(); j++) {
                String longTime = list.get(j).getPostposttime();

                try {
                    end = TimerUtils.ConverToDate(longTime);
                    str = TimerUtils.getCurrentDate();
                    begin = TimerUtils.stringToDate(str, "yyyy-MM-dd HH:mm:ss");
                    Date d = new Date(end.getTime() + 300000);
                    showDate = TimerUtils.formatRemainingTime(begin, d);
                    list.get(j).setQtime(showDate);
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    // e1.printStackTrace();
                }
            }
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).getQtime().equals("0秒")) {
                    QiangDanItem q = list.get(j);
                    list.remove(j);
                    kjdb.delete(q);
                }
            }
            notifyDataSetChanged();

        }
    };

    public LimitAdapter(Context context, List<QiangDanItem> list, KJDB kjdb) {
        super();
        this.context = context;
        this.list = list;
        this.kjdb = kjdb;

    }

    public void dele(String orderid) {
        for (int j = 0; j < list.size(); j++) {
            if (list.get(j).getDingdannum().equals(orderid)) {
                QiangDanItem q = list.get(j);
                list.remove(j);
                kjdb.delete(q);
            }
        }
        notifyDataSetChanged();
    }

    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        if (list != null && position < list.size()) {
            return list.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            v = new ViewHolder();
            convertView = View.inflate(context, R.layout.qianglistitem, null);
            MyApplication.ScaleScreenHelper.loadView((ViewGroup) convertView);
            convertView.setTag(v);
        } else {
            v = (ViewHolder) convertView.getTag();
        }
        v.l_1 = (LinearLayout) convertView.findViewById(R.id.l_1);
        v.l_2 = (LinearLayout) convertView.findViewById(R.id.l_2);
        v.l_3 = (LinearLayout) convertView.findViewById(R.id.l_3);
        v.l_4 = (LinearLayout) convertView.findViewById(R.id.l_4);
        v.textView = (TextView) convertView.findViewById(R.id.textView39);
        v.textView6 = (TextView) convertView.findViewById(R.id.textView6);
        v.textView8 = (TextView) convertView.findViewById(R.id.textView8);
        v.textView10 = (TextView) convertView.findViewById(R.id.textView10);
        v.textView13 = (TextView) convertView.findViewById(R.id.textView13);
        v.textView37 = (TextView) convertView.findViewById(R.id.textView37);
        v.textView41 = (TextView) convertView.findViewById(R.id.textView41);
        v.textView40 = (TextView) convertView.findViewById(R.id.textView40);
//        v.textView38 = (TextView) convertView.findViewById(R.id.textView38);
        v.lin_qiang = (RelativeLayout) convertView.findViewById(R.id.lin_qiang);
        if (list.get(position).getOrdertype().equals("1")) {
            v.lin_qiang.setBackgroundColor(Color.parseColor("#f08b32"));
            v.textView6.setBackgroundColor(Color.parseColor("#d56c70"));
            v.textView6.setText("实时订单");
            v.l_1.setVisibility(View.GONE);
            v.textView8.setText("距离您"+list.get(position).getJili()+"km");
        }
        if (list.get(position).getOrdertype().equals("2")) {
            v.textView6.setText("预约订单");
            v.lin_qiang.setBackgroundColor(Color.parseColor("#69c7e3" +
                    ""));
            v.textView6.setBackgroundColor(Color.parseColor("#43cc82"));
            v.l_1.setVisibility(View.GONE);
            v.textView8.setText(list.get(position).getUsetime() + list.get(position).getWeek() + list.get(position).getTime());
        }
        if (list.get(position).getIsbackorder().equals("1")) {
            v.l_2.setVisibility(View.VISIBLE);
        }else{
            v.l_2.setVisibility(View.GONE);
        }
        if (list.get(position).getIsmove().equals("1")) {
            v.l_3.setVisibility(View.VISIBLE);
        }else{
            v.l_3.setVisibility(View.GONE);
        }
        if (list.get(position).getSaycontent().equals("1")) {
            v.l_4.setVisibility(View.VISIBLE);
        }else{
            v.l_4.setVisibility(View.GONE);
        }
        v.textView.setText(list.get(position).getQtime());

        v.textView10.setText(list.get(position).getShi());
        v.textView13.setText(list.get(position).getZhong());
        v.textView41.setText("¥" + list.get(position).getAccount() + "元" + "+" + list.get(position).getTip());
//        if (list.get(position).getTip().length() > 0) {
//            v.textView40.setVisibility(View.VISIBLE);
//            v.textView38.setText(list.get(position).getReward() + "元");
//        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    Intent i = new Intent();
                    i.setClass(context, GuangBoActivity.class);
                    i.putExtra("orderid", list.get(position).getDingdannum());
                    i.putExtra("uid", list.get(position).uid);
                    System.out.println(list.get(position).getDingdannum());
                    context.startActivity(i);

            }


        });
        return convertView;
    }

    //
    public void start() {
        isPlay = true;
        runnable.run();
    }

    public void stop() {
        isPlay = false;
    }

    public class ViewHolder {
        public TextView textView;
        public TextView textView6;
        public TextView textView8;
        public TextView textView10;
        public TextView textView13;
        public TextView textView37;
        public TextView textView41;
        public TextView textView40;
        public TextView textView38;
        public RelativeLayout lin_qiang;
        public LinearLayout l_1, l_2, l_3, l_4;
    }
}
